package com.puman.watchtrade.fragment.home;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;

import com.pingplusplus.android.PaymentActivity;
import com.puman.watchtrade.MainActivity;
import com.puman.watchtrade.R;
import com.puman.watchtrade.fragment.home.adapter.HomeItemGridViewAdapter;
import com.puman.watchtrade.fragment.home.model.HomeItem;
import com.puman.watchtrade.fragment.home.model.HomeItems;
import com.puman.watchtrade.fragment.sell.SellFragment;
import com.puman.watchtrade.util.BaseFragment;

/*
 */
@SuppressLint({ "ValidFragment", "NewApi" })
public class HomeFragment extends BaseFragment implements OnClickListener {
	private View mView;

	/*
	 * init UI
	 */
	private GridView gridView;
	private RelativeLayout titleLayout;
	/*
	 * init variable
	 */
	private HomeItemGridViewAdapter homeItemGridViewAdapter;
	private HomeItems homeItems = new HomeItems();
	public int titlelayoutHeight = 0;
	private HomeFragment homeFragment;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	RelativeLayout homelLayout;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.home_layout, container, false);
		initView();
		return mView;

	}

	

	private void initView() {

		homelLayout = (RelativeLayout) mView.findViewById(R.id.home_rlayout);
		homeFragment = this;
		mView.findViewById(R.id.main_left_imgbtn).setOnClickListener(
				MainActivity.getInstant().mySetOnClickListener);

		gridView = (GridView) mView.findViewById(R.id.gridView);
		homeItemGridViewAdapter = new HomeItemGridViewAdapter(getActivity(),
				homeFragment, homeItems);
		gridView.setAdapter(homeItemGridViewAdapter);
		initItem();
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// Animation anim = AnimationUtils.loadAnimation(
				// MainActivity.getInstant(), R.anim.myanim);
				// homelLayout.startAnimation(anim);
				FragmentTransaction transaction = MainActivity.fragmentManager
						.beginTransaction();
				VarietiesListFragment sellFragment = new VarietiesListFragment(
						homeItems.homeItemList.get(position).getId(),
						homeItems.homeItemList.get(position).getTitle());
				transaction.replace(R.id.main_non_bottom, sellFragment);
				transaction.addToBackStack(null);
				transaction.commit();
			}
		});

		titleLayout = (RelativeLayout) mView.findViewById(R.id.title_relative);
		LayoutParams layoutParams = (LayoutParams) titleLayout
				.getLayoutParams();
		titlelayoutHeight = layoutParams.height;

		mView.findViewById(R.id.sold_layout).setOnClickListener(this);

	}

	private void initItem() {
		homeItems.homeItemList.clear();
		HomeItem homeItem = new HomeItem();
		homeItem.setId("1");
		homeItem.setTitle("钟表");
		homeItem.setImgUrl(R.drawable.home_item1);
		homeItems.homeItemList.add(homeItem);
		homeItem = new HomeItem();
		homeItem.setId("2");
		homeItem.setTitle("首饰");
		homeItem.setImgUrl(R.drawable.home_item2);
		homeItems.homeItemList.add(homeItem);
		homeItem = new HomeItem();
		homeItem.setId("3");
		homeItem.setTitle("包袋");
		homeItem.setImgUrl(R.drawable.home_item3);
		homeItems.homeItemList.add(homeItem);
		homeItem = new HomeItem();
		homeItem.setId("4");
		homeItem.setTitle("其他");
		homeItem.setImgUrl(R.drawable.home_item4);
		homeItems.homeItemList.add(homeItem);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.sold_layout:
			FragmentTransaction transaction = MainActivity.fragmentManager
					.beginTransaction();
			SellFragment sellFragment = new SellFragment(true);
			transaction.replace(R.id.main_non_bottom, sellFragment);
			transaction.addToBackStack(null);
			transaction.commit();
			break;

		default:
			break;
		}

	}

}
